package com.kehutong.charge.admin;

import com.kehutong.charge.entity.MeterClassify;
import com.kehutong.charge.service.MeterClassifyService;
import com.kehutong.charge.service.MeterTypeService;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import java.util.List;

/**
 * @program: kht
 * @description: 仪表分类
 * @author: joneron
 * @create: 2021/6/21
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/meter/classify")
public class MeterClassifyController {

    private static final Logger logger = LoggerFactory.getLogger(MeterClassifyController.class);

    @Inject
    DeptService deptService;

    @Inject
    MeterClassifyService meterClassifyService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:meter:type:view")
    public Object page(JSONObject jsonObject) {
        return meterClassifyService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("pay:charge:meter:type:view")
    public Object list(JSONObject jsonObject) {
        return meterClassifyService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/save")
    public Object save(MeterClassify entity, Token token) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        return meterClassifyService.save(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/update")
    public Object update(MeterClassify entity) throws Exception {

        return meterClassifyService.update(entity);
    }

    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/delete")
    public Object delete(MeterClassify entity) {

        return meterClassifyService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(MeterClassify entity) {

        return meterClassifyService.get(entity);
    }

}
